<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="wrap">
    <button class="btn">替换</button>
    <div class="list">
      <li class="first">01</li>
      <li>02</li>
      <li class="mid">03</li>
      <li>04</li>
      <li>05</li>
      <li class="last">06</li>
      <!-- <li>07</li> -->
    </div>
  </div>
</body>
<script>
  // 节点的替换
  // parent.replaceChild(newNode,oldNode)   喜新厌旧 由父元素统筹 用新的节点 替换 已存在的节点    



  var list = document.getElementsByClassName("list")[0];
  var mid = document.getElementsByClassName("mid")[0];
  var btn = document.getElementsByClassName("btn")[0];


  // 点击新增按钮 创建li => 替换mid
  btn.onclick = function () {
    var li = document.createElement("li");
    // 添加属性
    li.setAttribute("class", "newLi");
    li.setAttribute("title", "hello");

    // 创建文本节点
    var text = document.createTextNode("我是新增的li");
    // console.log(text,typeof text);
    li.appendChild(text);   //把文本节点放到 li中

    console.log(li);

    // 重复替换也会报错 (The node to be replaced is not a child of this node. 被替换的节点不是当前父节点的子节点)
    if (mid) {
      list.replaceChild(li, mid);
      mid = null;
    }
  }




</script>

</html>